package utils

import (
	"fmt"
	"os"
)

const (
	B float64 = 1024 << (iota * 10)
	KB
	MB
	GB
	TB
	PB
)

func FileSizeWithUint64(size uint64) string {
	return FileSize(float64(size))
}

func FileSize(size float64) string {
	unitArr := []string{"B", "KB", "MB", "GB", "TB", "PB"}
	unit := unitArr[0]
	i := 0
	tmp := size
	for tmp >= B {
		tmp /= 1024
		i++
		if i >= len(unitArr) {
			unit = unitArr[len(unitArr)-1]
			break
		}
		unit = unitArr[i]
	}
	return fmt.Sprintf("%.2f%s", tmp, unit)
}

func Exist(path string) (os.FileInfo, bool) {
	info, err := os.Stat(path)
	return info, err == nil
}

func ExistFile(path string) bool {
	info, exist := Exist(path)
	return exist && !info.IsDir()
}

func ExistDir(path string) bool {
	info, exist := Exist(path)
	return exist && info.IsDir()
}
